package com.yjhb.android.sbz.activity

import android.graphics.Color
import android.view.View
import com.bumptech.glide.Glide
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.adapter.BlackListAdapter
import com.yjhb.android.sbz.mvp.blacklist.BlacklistPresenter
import com.yjhb.android.sbz.mvp.blacklist.BlacklistView
import com.jojo.android.zxlib.base.BaseRecyclerAdapterV2
import com.jojo.android.zxlib.tool.ZxToast
import com.tencent.imsdk.v2.V2TIMUserFullInfo
import kotlinx.android.synthetic.main.activity_blacklist.*
import kotlinx.android.synthetic.main.item_blacklist.*

class BlacklistActivity : BaseActivity<BlacklistPresenter>(), BlacklistView {

    private val adapter = BlackListAdapter(this)
    private val data = mutableListOf<V2TIMUserFullInfo>()
    private var userId: String? = null

    override fun initPresenter(): BlacklistPresenter {
        return BlacklistPresenter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_blacklist
    }

    override fun initUI() {
        userId = intent.getStringExtra("userId")
        emptyRecyclerView.emptyView = emptyView
        emptyRecyclerView.adapter = adapter
        adapter.data = data
    }

    override fun initData() {
        emptyView.showLoading()
        presenter.getBlackList()
    }

    override fun regEvent() {
        adapter.setOnItemClick(object : BaseRecyclerAdapterV2.ItemClickListener<V2TIMUserFullInfo?> {
            override fun onViewClick(view: View?, msg: String?, index: Int, data: V2TIMUserFullInfo?) {
                if (msg == "state") {
                    data?.userID?.let { it1 -> presenter.deleteBlack(it1) }
                }
            }

            override fun onItemClick(index: Int, data: V2TIMUserFullInfo?) {

            }
        })
        emptyView.setOnReloadClickListener { initData() }
        back?.setOnClickListener { finish() }
    }

    override fun onWait() {
        loadingDialog.show()
    }

    override fun onFailure(code: Int, msg: String?) {
        ZxToast.showBottomToast(msg)
        loadingDialog.cancel()
    }

    override fun onListLoadFail(code: Int, msg: String) {
        this.data.clear()
        adapter.notifyDataSetChanged()
        emptyView.showFail(msg, true)
    }

    override fun onBlackList(data: List<V2TIMUserFullInfo>) {
        this.data.clear()
        this.data.addAll(data)
        //获取到黑名单列表后再获取该联系人的信息
        userId?.let {
            presenter.getFriendInfo(it)
        }
    }

    override fun onSuccess(type: Int) {
        loadingDialog.cancel()
        when (type) {
            1 -> ZxToast.showBottomToast("已拉黑")
            2 -> ZxToast.showBottomToast("已移除")
        }
        initData()
    }

    override fun onFriendInfo(info: V2TIMUserFullInfo) {
        item_blacklist.visibility = View.VISIBLE
        Glide.with(this)
                .load(info.faceUrl)
                .error(R.mipmap.test_head)
                .into(head_iv)
        name_tv.text = if (info.nickName == null || info.nickName == "") info.userID else info.nickName
        var b = false
        for (i in data) {
            if (i.userID == info.userID) {
                b = true
            }
        }
        if (b) {
//            state_tv.setTextColor(Color.parseColor("#AAAAAA"))
            state_tv.text = "移除"
            state_tv.setOnClickListener { userId?.let { it1 -> presenter.deleteBlack(it1) } }
        } else {
//            state_tv.setTextColor(Color.parseColor("#000000"))
            state_tv.text = "加入"
            state_tv.setOnClickListener { userId?.let { it1 -> presenter.addBlack(it1) } }
        }
        emptyView.emptyTipsTv.setTextColor(Color.parseColor("#AAAAAA"))
        emptyView.showTips("黑名单空空如也哦~~")
        adapter.notifyDataSetChanged()
    }
}